package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.FaceDesignOrder;
import com.yami.shop.bean.param.FaceDesignOrderParam;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.FaceDesignOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 人脸设计稿订单管理控制器
 * @author
 */
@RestController
@RequestMapping("/admin/faceDesignOrder")
@Tag(name = "人脸设计稿订单管理")
@AllArgsConstructor
public class AdminFaceDesignOrderController {

    private final FaceDesignOrderService faceDesignOrderService;

    /**
     * 分页获取人脸设计稿订单列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页获取人脸设计稿订单列表")
//    @PreAuthorize("@pms.hasPermission('admin:faceDesignOrder:page')")
    public ServerResponseEntity<IPage<FaceDesignOrder>> page(
            FaceDesignOrderParam orderParam,
            @Parameter(description = "分页参数") PageParam<FaceDesignOrder> page) {

        IPage<FaceDesignOrder> faceDesignOrderPage = faceDesignOrderService.pageByOrderParam(page, orderParam);
        return ServerResponseEntity.success(faceDesignOrderPage);
    }

    /**
     * 根据订单号获取订单详情
     */
    @GetMapping("/info/{orderNumber}")
    @Operation(summary = "根据订单号获取订单详情")
//    @PreAuthorize("@pms.hasPermission('admin:faceDesignOrder:info')")
    public ServerResponseEntity<FaceDesignOrder> info(@PathVariable("orderNumber") String orderNumber) {
        FaceDesignOrder faceDesignOrder = faceDesignOrderService.getByOrderNumber(orderNumber);
        return ServerResponseEntity.success(faceDesignOrder);
    }

    /**
     * 确认排单
     */
    @PutMapping("/confirmSchedule/{orderNumber}")
    @Operation(summary = "确认排单")
//    @PreAuthorize("@pms.hasPermission('admin:faceDesignOrder:confirmSchedule')")
    public ServerResponseEntity<Boolean> confirmSchedule(@PathVariable("orderNumber") String orderNumber) {
        boolean result = faceDesignOrderService.confirmSchedule(orderNumber);
        if (result) {
            return ServerResponseEntity.success(true);
        } else {
            return ServerResponseEntity.showFailMsg("确认排单失败");
        }
    }

    /**
     * 确认支付
     */
//    @PutMapping("/confirmPay/{orderNumber}")
//    @Operation(summary = "确认支付")
//    @PreAuthorize("@pms.hasPermission('admin:faceDesignOrder:confirmPay')")
//    public ServerResponseEntity<Boolean> confirmPay(@PathVariable("orderNumber") String orderNumber) {
//        boolean result = faceDesignOrderService.payOrder(orderNumber);
//        if (result) {
//            return ServerResponseEntity.success(true);
//        } else {
//            return ServerResponseEntity.showFailMsg("确认支付失败");
//        }
//    }

    /**
     * 发货
     */
    @PutMapping("/ship/{orderNumber}")
    @Operation(summary = "发货")
//    @PreAuthorize("@pms.hasPermission('admin:faceDesignOrder:ship')")
    public ServerResponseEntity<Boolean> shipOrder(
            @PathVariable("orderNumber") String orderNumber,
            @RequestParam String expressCompany,
            @RequestParam String trackingNumber) {
        boolean result = faceDesignOrderService.shipOrder(orderNumber, expressCompany, trackingNumber);
        if (result) {
            return ServerResponseEntity.success(true);
        } else {
            return ServerResponseEntity.showFailMsg("发货失败");
        }
    }

    /**
     * 确认退款
     */
    @PutMapping("/confirmRefund/{orderNumber}")
    @Operation(summary = "确认退款")
//    @PreAuthorize("@pms.hasPermission('admin:faceDesignOrder:confirmRefund')")
    public ServerResponseEntity<Boolean> confirmRefund(@PathVariable("orderNumber") String orderNumber) {
        boolean result = faceDesignOrderService.confirmRefund(orderNumber);
        if (result) {
            return ServerResponseEntity.success(true);
        } else {
            return ServerResponseEntity.showFailMsg("确认退款失败");
        }
    }

    /**
     * 取消订单
     */
    @PutMapping("/cancel/{orderNumber}")
    @Operation(summary = "取消订单")
//    @PreAuthorize("@pms.hasPermission('admin:faceDesignOrder:cancel')")
    public ServerResponseEntity<Boolean> cancelOrder(
            @PathVariable("orderNumber") String orderNumber,
            @RequestParam(required = false) String cancelReason) {
        boolean result = faceDesignOrderService.cancelOrder(orderNumber, null, cancelReason);
        if (result) {
            return ServerResponseEntity.success(true);
        } else {
            return ServerResponseEntity.showFailMsg("取消订单失败");
        }
    }
}
